#coding: utf8
import re
from pygeocoder import Geocoder
import time
import urlparse
from scrapy.http import Request
from scrapy.http import FormRequest
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from fresh.items import RestaurantItem
from fresh.items import RestaurantReview
from decimal import *
from datetime import datetime

class TenedorUtils(object):

    utils = None
    dictCalidadP = {"Muy interesante": "90","Interesante":"70","Correcto":"50","Caro":"30"}
    dictNoise = {"Muy tranquilo": "90","Tranquilo":"88","Moderado":"66"}
    CommentNull = 'No hay opiniones'

    def fillRestaurant(self,response,hxs,restaurant):
        try:
            
            restaurant['city']= 'NA'
            restaurant['restaurant_name'] = hxs.select('(//h1[@itemprop="name"]/text())').extract()

            address0 = hxs.select('//span[@itemprop="streetAddress"]/text()').extract()
            address1 = hxs.select('//span[@itemprop="addressLocality"]/text()').extract()
            address = self.utils.convertListToString(address0) + ", " + self.utils.convertListToString(address1)
            restaurant['address'] = address
            restaurant['address_google'] = self.utils.getRestAddressGoogleReady(address)
            restaurant['cp'] = hxs.select('//span[@itemprop="postalCode"]/text()').extract()
            restaurant['url_scrapy'] = response.url
            
            #restaurant['reviews_total']= self.__getTotalReviews(self.utils.convertListToString(hxs.select('(//div[@id="rate_info"]/div[@class="hr_r"]/span/text())').extract()))
            restaurant['reviews_total']= self.__getTotalReviews(self.utils.convertListToString(hxs.select('(//select[@id="lafourchette_ratefilter_form_is_special_offer"]/option[@value="ALL"]/text())').extract()))

            restaurant['reviews'] = [] 
            
            restaurant['score'] = self.__getScore(hxs.select('(//div[@id="rate_info"]/div[@class="hr_r"]/div[@class="icon rate-big rating"]/span/text())').extract())

            restaurant['score_cocina'] = self.__getSubScore(hxs.select('//div[@class="span5"]/text()[contains(.,"Cocina")]/../../div[@class="span1"]/text()').extract())
            restaurant['score_servicio'] = self.__getSubScore(hxs.select('//div[@class="span5"]/text()[contains(.,"Servicio")]/../../div[@class="span1"]/text()').extract())
            restaurant['score_ambiente'] = self.__getSubScore(hxs.select('//div[@class="span5"]/text()[contains(.,"Ambiente")]/../../div[@class="span1"]/text()').extract())

            restaurant['score_calidadP'] = self.__getCalidadP(hxs.select('//div[@class="span5"]/text()[contains(.,"calidad-precio")]/../../div[@class="span5 rate"]/text()').extract())
            restaurant['score_animacion'] = self.__getNoise(hxs.select('//div[@class="span5"]/text()[contains(.,"Nivel de ruido")]/../../div[@class="span5 rate"]/text()').extract())
            restaurant['score_limpieza'] = None
            restaurant['cp'] = None
            

            """list of types of restaurant"""
            restaurant['rest_type']=self.__getRestType(hxs.select('//div/text()[contains(.,"Tipo de restaurante")]/../..'))
      
            return


        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurant from response: " + str(e),self.utils.ERROR)

    def __getTotalReviews(self,txtTotal):
        try:
            tmp_s=None
            tmp_s=str(txtTotal)
            tmp_s=tmp_s.replace("Todos (","")
            tmp_s=tmp_s.replace(")","")
            tmp_s=self.utils.cleanString(tmp_s.strip())
            if len(tmp_s)>0:            
                tmp_s=str(int(tmp_s))
            else:
                tmp_s=None
        except Exception,e:
            self.utils.logProcessDetail("Error in __getTotalReviews: " + str(e) + "valor: " + str(txtTotal),self.utils.ERROR)
        finally:
            return tmp_s
            


    def __getCalidadP(self,calidadP):
        score = None
        if calidadP is not None:
            txtCalidadP = self.utils.convertListToString(calidadP)
            if len(txtCalidadP)>0:
                score = self.dictCalidadP.get(txtCalidadP)
                if score is None:
                    self.utils.logProcessDetail("Error no se encuentra la clave en diccionario de calidadP: " + str(txtCalidadP),self.utils.ERROR)
                
        return score


    def __getNoise(self,noise):

        score = None

        if noise is not None:
            txtNoise = self.utils.convertListToString(noise)
            if len(txtNoise)>0:
                score = self.dictNoise.get(txtNoise)
                if score is None:
                    self.utils.logProcessDetail("Error no se encuentra la clave en diccionario de Noise: " + str(txtNoise),self.utils.ERROR)

        return score


    def __getRestType(self,hxs):
        try:
            d=dict()
            for rest_type in hxs.select('ul/a/text()').extract():
                sRest_type = self.utils.convertListToString(rest_type)
                d[sRest_type]=0
            return d.keys()      

        except Exception,e:
            self.utils.logProcessDetail("Error in __getRestType: " + str(e),self.utils.ERROR)

    def fillRestaurantComments(self,hxs,restaurant):
        try:
            """
            from scrapy.shell import inspect_response
            inspect_response(response)
            """

            #position = 0
            for list in hxs.select('//div[@class="avis m_y hr_b"]/div[@class="row-fluid"]/div[@class="span9"]'):
                #position = position +1

                #posts with comments
                bComment=False
                for rev in list.select('div[@class="bg"]/p[@itemprop="review"]'):
                    bComment=True
                    restaurantReview = RestaurantReview()
                    restaurantReview['date'] = self.__getCommentDate(self.utils.convertListToString(list.select('small/text()').extract()))
                    restaurantReview['author'] = rev.select('span[@class="bold"]/text()').extract()
                    if restaurantReview['author'] is None or len(restaurantReview['author'])==0:
                        restaurantReview['author']=list.select('../div[@class="span2"]/div[@class="m_x"]/div[@class="bold"]/text()').extract()
                    
                    restaurantReview['score'] = self.__getScore(rev.select('small[@itemprop="reviewRating"]/span[@itemprop="ratingValue"]/text()').extract())
                    if restaurantReview['score'] is None or len(restaurantReview['score'])==0:
                        restaurantReview['score'] = self.__getScore(rev.select('small[@class="rate"]/span[@class="bigger"]/text()').extract())
                        
                    restaurantReview['comment'] = self.__getComment(self.utils.convertListToString(rev.select('span[@itemprop="reviewBody"]/text()').extract()))
                    restaurant['reviews'].append(restaurantReview)

                #check if posts without comments
                if not bComment:
                    txt = self.utils.convertListToString(list.select('div[@class="bg"]/p/text()').extract())
                    if txt is not None and len(str(txt))>0:
                        restaurantReview = RestaurantReview()
                        restaurantReview['date'] = self.__getCommentDate(self.utils.convertListToString(list.select('small/text()').extract()))
                        restaurantReview['author'] = list.select('../div[@class="span2"]/div[@class="m_x"]/div[@class="bold"]/text()').extract()                    
                        restaurantReview['score'] = self.__getScore(list.select('div[@class="icon rate rating"]/span/text()').extract())
                        restaurantReview['comment'] = self.__getComment(txt)
                        restaurant['reviews'].append(restaurantReview)
            return

        except Exception,e:
            self.utils.logProcessDetail("Error in fillRestaurantComments from response: " + str(e),self.utils.ERROR)

    def __getComment(self,txtPost):
        txtRet=txtPost
        if str(txtPost)==str(self.CommentNull):
            txtRet='NA'
        else:
            self.utils.logProcessDetail("txtPost: "+txtPost + " comment: " + str(self.CommentNull),self.utils.INFO)
        return txtRet
            
    

    def __getCommentDate(self,txtDate):
        tmp_s=None
        try:
            if len(txtDate)>0:
                po=txtDate.find("/")
                if po != -1:
                    tmp_s=txtDate[po-2:]
                    date = datetime.strptime(tmp_s , '%d/%m/%Y')
                    tmp_s = date.strftime('%d/%m/%Y')
                    return tmp_s

        except Exception,e:
            self.utils.logProcessDetail("Error in __getCommentDate: " + str(e),self.utils.ERROR)
        
    def __getSubScore(self,txtScore):
        tmp_s=None
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            try:
                tmp_s=str(Decimal(tmp_s)*10)
            except Exception,e:
                """probably comma is wrong"""
                tmp_s=tmp_s.replace(",",".")
                tmp_s=str(Decimal(tmp_s)*10)
               
        return tmp_s   

    def __getScore(self,txtScore):
        tmp_s=None
        if len(txtScore)>0:
            tmp_s = str(txtScore[0])
            try:
                tmp_s=str(Decimal(tmp_s))
            except Exception,e:
                """probably comma is wrong"""
                tmp_s=tmp_s.replace(",",".")
                tmp_s=str(Decimal(tmp_s))
               
        return tmp_s           


        